import { isInBlackList } from "./synthesisLogic";

Window_SynthesisItemList.prototype.drawItemNumber = function (
  item,
  x,
  y,
  width
) {
  if (!DataManager.isIndependent(item)){
    if (!this.needsNumber()) return;
    var numItems = Yanfly.Util.toGroup(this.itemNum(item));
    this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
    // this.drawText('\u00d7' + numItems, x, y, width, 'right');
    this.drawText(numItems, x, y, width, 'right')
    this.resetFontSettings();
  }
    
};

Window_SynthesisItemList.prototype.drawItemCost = function () { };

Window_SynthesisItemList.prototype.realItem = function(item) {
  return item;  //炼妖界面不区分是否是可使用装备，直接显示本身即可，不必读取对应的物品
};
Window_SynthesisItemList.prototype.includes = function (item) {
  if (!item) return false;

  const categoryFilterRules = [
    {
      name: "物品",
      condition: () => {
        if (!!item) return DataManager.isItem(item) && item.id < 120;
      },
    },
    {
      name: "装备",
      condition: () => {
        if (!!item)
          return (
            DataManager.isWeapon(item) ||
            (DataManager.isArmor(item) && !item.itemCategory.contains("护驾"))
          );
      },
    },
    {
      name: "护驾",
      condition: () => {
        if (!!item) return item.itemCategory.contains("护驾");
      },
    },
  ];


  // 没有catch level的
  if (
    this._itemTypeId !== 2 &&
    item.meta &&
    item.meta["Catch Level"] === undefined
  ) {
    return false
  }

  // 不是物品、装备、护驾
  if (this._itemTypeId < 0 || this._itemTypeId >= categoryFilterRules.length)
    return false;


  let condition = false;
  try {
    condition = categoryFilterRules[this._itemTypeId].condition();
  } catch (err) {
    console.error(err);
    condition = false;
  }

  const isBlackListItem = isInBlackList(this._itemTypeId, item)
  return condition && !isBlackListItem && this.itemNum(item) > 0;
};


Window_SynthesisItemList.prototype.drawItemLevel = function(item, dx, dy, dw) {
  let level = 0;
      level = item.catchLevel;
      if (item.useItemId){
          if($dataItems[item.useItemId].petAddedInfo){
              level = $dataEnemies[$dataItems[item.useItemId].petAddedInfo.enemyId].catchLevel;
          }
      }
      this.setSmallerFont();
      this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
      this.drawText(level + TextManager.levelA, dx, dy, dw, 'right');
      this.resetFontSettings();

};


Window_SynthesisItemList.prototype.selectLast = function() {
  if(ConfigManager.commandRemember){
    var index = this.baseData().indexOf($gameParty.lastSynthesisItem());
    this.select(index >= 0 ? index : 0);
  }
};


// 获取列表的baseItem。
Window_SynthesisItemList.prototype.baseData = function() {
  return this._data.map(function(item) {
    if(!!item) return DataManager.getBaseItem(item);
  });
};

// 物品列表排序
const originSynthItemListMakeItemList = Window_SynthesisItemList.prototype.makeItemList;
Window_SynthesisItemList.prototype.makeItemList = function() {
    originSynthItemListMakeItemList.call(this);
    // 物品排序
    this._data.sort(function (a, b) {
        if (!a || !b)
            return
        // 护驾按等级或种族排序
        if (a.itemCategory.contains('护驾') && a.itemCategory.contains('护驾')) {
            if (!a.enemyId || !b.enemyId)
                return
            if ($dataEnemies[a.enemyId] == undefined || $dataEnemies[b.enemyId] == undefined)
                return
            if (ConfigManager['enemySort']) {
                // 等级优先排序
                if (!$dataEnemies[a.enemyId].catchLevel || !$dataEnemies[b.enemyId].catchLevel)
                    return
                if ($dataEnemies[b.enemyId].catchLevel - $dataEnemies[a.enemyId].catchLevel !== 0) {
                    return $dataEnemies[b.enemyId].catchLevel - $dataEnemies[a.enemyId].catchLevel
                } else {
                    if (!$dataEnemies[a.enemyId].classId || !$dataEnemies[b.enemyId].classId)
                        return
                    return $dataEnemies[a.enemyId].classId - $dataEnemies[b.enemyId].classId
                }
            } else {
                // 种族优先排序
                if (!$dataEnemies[a.enemyId].classId || !$dataEnemies[b.enemyId].classId)
                    return
                if ($dataEnemies[a.enemyId].classId - $dataEnemies[b.enemyId].classId !== 0) {
                    return $dataEnemies[a.enemyId].classId - $dataEnemies[b.enemyId].classId

                } else {
                    if (!$dataEnemies[a.enemyId].catchLevel || !$dataEnemies[b.enemyId].catchLevel)
                        return
                    return $dataEnemies[b.enemyId].catchLevel - $dataEnemies[a.enemyId].catchLevel
                }
            }
        // 非护驾
        }else{
          // 等级优先排序
          if (ConfigManager['enemySort']) 
            if(b.catchLevel - a.catchLevel !==0 )
              return b.catchLevel - a.catchLevel
            else
              return DataManager.itemSortMapping(a.iconIndex) - DataManager.itemSortMapping(b.iconIndex)
          // 类型优先排序
          else
            if(DataManager.itemSortMapping(a.iconIndex) - DataManager.itemSortMapping(b.iconIndex) !==0 )
              return DataManager.itemSortMapping(a.iconIndex) - DataManager.itemSortMapping(b.iconIndex)
            else
              return b.catchLevel - a.catchLevel
        }
    });
  };